<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合同编号" prop="contractNum">
        <el-input
          v-model="queryParams.contractNum"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款主体" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table ref="multipleTable" v-loading="loading" :data="sellManList">
      <el-table-column label="序号" align="center" prop="id" width="50">
        <template slot-scope="scope">
          <div>
            {{
              scope.$index +
              (queryParams.pageNum - 1) * queryParams.pageSize +
              1
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="合同编号"
        align="center"
        prop="contractNum"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="收款主体"
        align="center"
        prop="main"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="合同金额(元)"
        align="center"
        prop="contractAmount"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="保证金(元)"
        align="center"
        prop="ensureAmount"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="交易流水号"
        align="center"
        prop="serialNumber"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="付款时间"
        align="center"
        prop="paymentTime"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        prop="opration"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-more"
            @click="handleedit('more', scope.row)"
            >更多</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      :title="title"
      :visible="open"
      width="780px"
      append-to-body
      :before-close="handeclose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="20" :offset="1">
            <el-form-item label="合同名称:" prop="contractName">
              <el-input
                v-model="form.contractName"
                :disabled="isdisable"
                placeholder="请输入"
              />
            </el-form-item>
            <el-form-item label="合同编号:" prop="contractNum">
              <el-input
                v-model="form.contractNum"
                :disabled="isdisable"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-form-item label="经办人:" prop="operateMan">
              <el-input
                v-model="form.operateMan"
                :disabled="isdisable"
                placeholder="请输入"
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label="签约日期:" prop="signDate">
              <el-date-picker
                v-model="form.signDate"
                type="date"
                placeholder="选择日期"
                style="width: 200px"
                :disabled="isdisable"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="保证金:" prop="ensureAmount">
              <el-input-number
                v-model="form.ensureAmount"
                style="width: 200px"
                :disabled="isdisable"
                :min="0"
                label="请输入"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="收款主体:" prop="collectMoneyBody">
              <el-input
                v-model="form.collectMoneyBody"
                placeholder="请输入"
                :disabled="isdisable"
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label="交易流水号:" prop="serialNumber">
              <el-input
                v-model="form.serialNumber"
                :disabled="isdisable"
                placeholder="请输入"
                style="width: 200px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-form-item label="合同金额(元):" prop="aomunt">
              <el-input-number
                v-model="form.aomunt"
                style="width: 200px"
                :disabled="isdisable"
                :min="0"
                label="请输入"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="供货截止时间:" prop="buyMantime">
              <el-date-picker
                :disabled="isdisable"
                v-model="form.buyMantime"
                type="date"
                placeholder="选择日期"
                style="width: 200px"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="手续费比例(%):" prop="serviceCharge">
              <el-input-number
                v-model="form.serviceCharge"
                style="width: 200px"
                :disabled="isdisable"
                :min="0"
                label="请输入"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="平台手续费(元):" prop="platformFee">
              <el-input-number
                v-model="form.platformFee"
                style="width: 200px"
                :disabled="isdisable"
                :min="0"
                label="请输入"
              ></el-input-number>
            </el-form-item>
            <!-- <el-form-item label="付款凭证:" prop="serialNumber">
              <el-input
                v-model="form.serialNumber"
                placeholder="请输入"
                :disabled="isdisable"
                style="width: 200px"
              />
            </el-form-item> -->
          </el-col>
          <el-col :span="20" :offset="1">
            <el-form-item label="合同期限:" prop="contractperiod">
              <el-date-picker
                v-model="form.contractperiod"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="20" :offset="1">
            <div>采购商</div>
            <el-form-item label="选择用户:" prop="buyMan">
              <el-select
                v-model="form.buyMan"
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="item in sellManoptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20" :offset="1">
            <div>供应商</div>
            <el-form-item label="选择用户:" prop="sellMan">
              <el-select
                v-model="form.sellMan"
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="item in sellManoptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20" :offset="1">
            <el-form-item label="上传合同附件:" prop="contractUrl">
              <FileUpload
                :limit="1"
                :fileSize="fileSize"
                :isShowTip="false"
                uploadtype="image"
                :fileList.sync="form.contractUrl"
                ref="tableupload"
                v-if="!isdisable"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="审核" :visible.sync="msgvisible" width="30%">
      <el-form
        ref="formtwo"
        :model="formtwo"
        :rules="rulestwp"
        label-width="100px"
      >
        <el-form-item label="审核:" prop="status">
          <el-radio v-model="formtwo.status" label="2">通过</el-radio>
          <el-radio v-model="formtwo.status" label="1">不通过</el-radio>
        </el-form-item>
        <el-form-item
          label="驳回原因:"
          prop="spfmt"
          v-if="formtwo.status == '1'"
        >
          <el-input
            type="textarea"
            placeholder="请输入"
            v-model="formtwo.explain"
            maxlength="200"
            show-word-limit
            :autosize="autosize"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="msgvisible = false">取 消</el-button>
        <el-button type="primary" @click="msgvisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  amountlist,
  profitlist,
  webuserlist,
  contractadd,
} from "@/api/business";
import Editor from "@/components/Editor";
import selfDirective from "@/utils/selfDirective";
import FileUpload from "@/components/FileUpload/index";
import ImageUpload from "@/components/ImageUpload/index";

export default {
  name: "sellMan",
  components: {
    Editor,
    FileUpload,
    ImageUpload,
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      // 遮罩层
      loading: false,
      routername: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fileSize: 0.5,
      // 公告表格数据
      sellManList: [
        {
          title: "TX2021001010",
          businessName: "天路电商",
          withdrawal: 10,
          ServiceCharge: 10,
          arrival: 10,
          updateTime: "2020-01-02",
          bankaccount: "6222021901024548990",
          bankname: "中国工商银行",
          accountname: "湖南星图广告有限公司",
          IBAN: "无",
          SwiftCode: "ICBKCNBJHUN",
          branchCode: "无",
          bankaddress: "长沙市芙蓉区五一大道100号",
        },
      ],
      // 弹出层合同名称
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawal: "0",
        status: "0",
      },
      // 表单参数
      form: {
        title: "",
        Productpicture: [],
      },
      // 表单校验
      rules: {
        contractName: [
          {
            required: true,
            message: "合同名称不能为空",
            trigger: "change",
          },
        ],
        signDate: [
          {
            required: true,
            message: "签约日期不能为空",
            trigger: "change",
          },
        ],
        operateMan: [
          {
            required: true,
            message: "经办人不能为空",
            trigger: "change",
          },
        ],
        ensureAmount: [
          {
            required: true,
            message: "保证金不能为空",
            trigger: "change",
          },
        ],
        collectMoneyBody: [
          {
            required: true,
            message: "收款主体不能为空",
            trigger: "change",
          },
        ],
        serialNumber: [
          {
            required: true,
            message: "交易流水号不能为空",
            trigger: "change",
          },
        ],
        contractNum: [
          {
            required: true,
            message: "合同编号不能为空",
            trigger: "change",
          },
        ],
        aomunt: [
          {
            required: true,
            message: "合同金额不能为空",
            trigger: "change",
          },
        ],
        buyMantime: [
          {
            required: true,
            message: "供货截止时间不能为空",
            trigger: "change",
          },
        ],
        serviceCharge: [
          {
            required: true,
            message: "手续费比例不能为空",
            trigger: "change",
          },
        ],
        platformFee: [
          {
            required: true,
            message: "平台手续费不能为空",
            trigger: "change",
          },
        ],
        contractperiod: [
          {
            required: true,
            message: "合同期限不能为空",
            trigger: "change",
          },
        ],
        buyMan: [
          {
            required: true,
            message: "采购商选择用户不能为空",
            trigger: "change",
          },
        ],
        sellMan: [
          {
            required: true,
            message: "供应商选择用户不能为空",
            trigger: "change",
          },
        ],
        contractUrls: [
          {
            required: true,
            message: "合同附件不能为空",
            trigger: "change",
          },
        ],
      },
      formtwo: {
        status: "2",
      },
      rulestwp: {
        status: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
      },
      issellMan: true,
      limit: 5,
      fileSize: 20,
      isShowTip: false,
      value: [],
      nationoptions: [],
      categoryoptions: [],
      companyoptions: [],
      gysTypeOptions: [],
      payEemerchantNooptions: [],
      isdisable: false,
      isdisabletwo: false,
      msgvisible: false,
      //银行信息
      bankmsg: {
        bankname: "",
        accountname: "",
        bankaccount: "",
        IBAN: "",
        SwiftCode: "",
        branchCode: "",
        bankaddress: "",
      },
      statusoptions: [
        {
          dictValue: "1",
          dictLabel: "已启用",
        },
        {
          dictValue: "2",
          dictLabel: "已禁用",
        },
      ],
      buyManoptions: [],
      sellManoptions: [],
    };
  },
  created() {
    if (this.$route.name == "/tosale") {
      this.routername = "tosale";
    } else if (this.$route.name == "/wanttobuy") {
      this.routername = "wanttobuy";
    }
    // this.getwebuserlist();
    this.getList();
    this.getDicts("gys_type").then((response) => {
      this.categoryoptions = response.data;
    });
    this.getDicts("gys-gj").then((response) => {
      this.nationoptions = response.data;
    });
    this.getDicts("pay-eemer-chantNo").then((response) => {
      this.payEemerchantNooptions = response.data;
    });
  },
  methods: {
    getwebuserlist() {
      webuserlist().then((res) => {
        let sellManoptions = [];
        let result = res.rows;
        result.map((item) => {
          var obj = {
            value: item.id,
            label: item.companyName,
          };
          sellManoptions.push(obj);
        });
        this.sellManoptions = sellManoptions;
      });
    },
    getbankaccount(row) {
      let bankaccount = "";
      if (row.bankaccount) {
        bankaccount =
          "*******" +
          row.bankaccount.slice(
            row.bankaccount.length - 4,
            row.bankaccount.length
          );
      } else {
        bankaccount = "";
      }
      return bankaccount;
    },
    handlecheck(row) {
      this.msgvisible = true;
    },
    /** 查询公告列表 */
    handelcopy() {
      copysellMan().then((res) => {
        this.msgSuccess("复制成功");
      });
    },
    /** 查询公告列表 */
    changeStatus(value) {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询公告列表 */
    handlecord(row) {
      this.msgvisible = true;
      this.bankmsg = row;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      if (this.$route.name == "/amountlist") {
        amountlist(this.queryParams).then((response) => {
          var sellManList = response.rows || [];
          sellManList.map((item) => {
            item.contractperiod = [item.beginTime, item.endTime];
          });
          this.sellManList = sellManList;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        profitlist(this.queryParams).then((response) => {
          var sellManList = response.rows || [];
          sellManList.map((item) => {});
          this.sellManList = sellManList;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.form = {
        id: undefined,
        contractUrl: [],
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editsellMan(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let form = this.form;
            let beginTime = form.contractperiod[0];
            let endTime = form.contractperiod[1];
            let contractUrl = JSON.stringify(form.contractUrl);
            var obj = {
              ...this.form,
              beginTime,
              endTime,
              contractUrl,
            };
            contractadd(obj).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleedit(type, row) {
      this.reset();
      if (type == "edit") {
        this.form = row;
        this.title = "修改";
        this.open = false;
        this.isdisabletwo = true;
      } else if (type == "add") {
        this.title = "新增";
        this.open = true;
      } else if (type == "more") {
        this.title = "详情";
        this.form = row;
        this.open = true;
        this.isdisabletwo = true;
      }
    },
    handeclose() {
      this.open = false;
    },
    getdictlabel(row, nationoptions, name) {
      let str = "";
      nationoptions.map((item) => {
        if (item.dictValue == row[name]) {
          str = item.dictLabel;
        }
      });
      return str;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除该仓库?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delsellMan(ids);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    changestate(row) {
      var that = this;
      const statestr = row.state == 1 ? "禁用" : "启用";
      this.$confirm("是否确认" + statestr + "该供应商?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        var obj = {
          id: row.id,
          state: row.state == 1 ? 0 : 1,
        };
        editsellMan(obj).then((response) => {
          that.$message.success(statestr + "成功");
          that.getList();
        });
      });
    },
  },
};
</script>
<style scoped lang="scss">
.radio-box {
  margin-bottom: 10px;
}
.stock {
  text-decoration: underline;
}
.copy {
  color: #999;
  cursor: pointer;
}
.box-card {
  margin-top: 20px;
  .card-title {
    font-weight: 700;
    font-style: normal;
    color: #555555;
  }
  .baseinfo {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: #999;
    background-color: #f2f2f2;
    padding-left: 10px;
    border-radius: 5px;
    margin-top: 10px;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
.lookcard {
  text-decoration: underline;
  color: #1890ff;
  cursor: pointer;
}
.banktable {
  td {
    padding: 5px;
  }
}

::v-deep {
  .el-table .cell {
    white-space: pre-line !important; /*保留换行符*/
  }

  .el-form-item__content {
    line-height: 30px;
  }
}
.formdata ::v-deep {
  .el-form-item__label {
    white-space: pre-line !important; /*保留换行符*/
    line-height: 15px;
  }
}
</style>